package com.coolw.data.migrate.process.strategy;

import com.coolw.data.migrate.common.constant.SqlTemplateConst;
import com.coolw.data.migrate.domain.request.DataMigrateRequest;
import com.coolw.data.migrate.domain.response.DataMigrateResponse;
import com.coolw.data.migrate.enums.MigrateTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 按指定日期迁移
 *
 * @author coolw
 * @date 2024/2/1 15:19
 */
@Slf4j
@Component
public class SpecifiedDateMigrateProcess extends AbstractDataMigrateProcess {

    @Override
    public MigrateTypeEnum type() {
        return MigrateTypeEnum.SPECIFIED_DATE;
    }

    @Override
    public DataMigrateResponse process(DataMigrateRequest req) {
        String specifiedDate = req.getSpecifiedDate();
        if (StrUtil.isBlank(specifiedDate)) {
            return DataMigrateResponse.fail("specifiedDate 不能为空");
        }
        return doProcess(req);
    }

    @Override
    public List<String> getDataMigrateSql(DataMigrateRequest req) {
        return getLastDataMigrateSql(req, perBatchLimit);
    }

    @Override
    public List<String> getLastDataMigrateSql(DataMigrateRequest req, long lastBatchSize) {
        String targetTableName = req.getTargetTableName();
        String sourceTableName = req.getSourceTableName();
        String specifiedDate = req.getSpecifiedDate();

        String insertSql = StrUtil.format(SqlTemplateConst.SpecifiedDateEnum.INSERT_SQL_TEMPLATE.getSql(), targetTableName, sourceTableName, specifiedDate, lastBatchSize);
        String deleteSql = StrUtil.format(SqlTemplateConst.SpecifiedDateEnum.DELETE_SQL_TEMPLATE.getSql(), sourceTableName, specifiedDate, lastBatchSize);
        return Arrays.asList(insertSql, deleteSql);
    }

    @Override
    public String getCountSql(DataMigrateRequest req) {
        String sourceTableName = req.getSourceTableName();
        String specifiedDate = req.getSpecifiedDate();
        return StrUtil.format(SqlTemplateConst.SpecifiedDateEnum.COUNT_SQL_TEMPLATE_WITH_CONDITION.getSql(), sourceTableName, specifiedDate);
    }
}
